/**
 * Created by kangdaye on 16/6/5.
 */

//Main
app.controller('orderDeliveryDialogCtr',["$scope","$rootScope","rowData","addEditModel","orderListService", function($scope,$rootScope,rowData,addEditModel,orderListService) {
    $scope.companyList = {};
    $scope.employeeInfo = null;
    $scope.rowData = rowData;
    $scope.getData = {
        orderNo : rowData.orderNo,
        deliveryCode : '',
        deliveryNumber : ''
    };


    $scope.selectCompany = function(code){
        if(code == 0000){
            $scope.getData.deliveryNumber = '';
            return;
        }
        $scope.employeeInfo = null;
    };

    $scope.submit = function(){
        if(addEditModel === 'update'){
            orderListService.updateDelivery($scope.getData,function(data){
                // $scope.closeThisDialog();  //dialog 关闭
                location.reload();
            });
            return;
        }
        orderListService.delivery($scope.getData,function(data){
            // $scope.closeThisDialog();  //dialog 关闭
            location.reload();
        });
    };

    $scope.$watch('employeeInfo',function(newVal,old){
        if(newVal){
            $scope.getData.deliveryNumber = newVal.originalObject.id;
        }
    });

    orderListService.companyList(function(data){
        $scope.companyList = data.data;
    });

    if(addEditModel === 'update'){
        orderListService.getDelivery({
            orderNo : rowData.orderNo
        },function(data){
            $scope.getData = data.data;
        });
    }
}]);
